#include "chairwindow.h"
#include "ui_chairwindow.h"



ChairWindow::ChairWindow(QWidget *parent, AdminData *newData) :
    QMainWindow(parent),
    ui(new Ui::ChairWindow)
{
    ui->setupUi(this);
    this->data = newData;
    ui->stackedWidget->setCurrentIndex(CHAIR_INDEX);
    courses = data->getCourses();
    for(int i = 0; i < courses.size(); i++){
        if(courses.at(i).getOutline().getStatus()=="approved"){
            ui->outlines->addItem(courses.at(i).getName());
            underReviewOutlines.append(const_cast<Course*>(&courses.at(i)));
        }
    }
}

ChairWindow::~ChairWindow()
{
    delete ui;
}

void ChairWindow::on_stackedWidget_currentChanged(int arg1)
{
    ui->editSectionStackedWidget->setCurrentIndex(arg1);
}

void ChairWindow::on_sectionListWidget_currentRowChanged(int currentRow)
{
    ui->editSectionStackedWidget->setCurrentIndex(currentRow);
}


void ChairWindow::on_outlines_itemDoubleClicked(QListWidgetItem *item)
{
    ui->stackedWidget->setCurrentIndex(EDIT_OUTLINE_INDEX);
    for (int i = 0; i < data->getOutlineSections().count(); ++i)
    {
        ui->editSectionStackedWidget->addWidget(new outlineSectionPage());
        if(ui->sectionListWidget->currentItem() == NULL)
            ui->sectionListWidget->addItem(data->getOutlineSections().at(i).getTitle());
        ui->editSectionStackedWidget->setCurrentIndex(i);
        if(underReviewOutlines.at(ui->outlines->currentRow())->getOutline().getOutlineSectionList().at(i).getText() != NULL){
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->instructorTextEdit->setText(underReviewOutlines.at(ui->outlines->currentRow())->getOutline().getOutlineSectionList().at(i).getText());
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->instructorTextEdit->setEnabled(false);
        }
        if(!data->getOutlineSections().at(i).getDescription().isNull()){
            qDebug() << "if";
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->descriptionTextEdit->setText(data->getOutlineSections().at(i).getDescription());
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->descriptionTextEdit->setEnabled(false);
        }
        if(data->getOutlineSections().at(i).getText() != NULL){
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->requiredTextEdit->setEnabled(false);
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->requiredTextEdit->setText(data->getOutlineSections().at(i).getText());
        }

        ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->openScheduledDialogButton->close();

    }
}



void ChairWindow::on_saveOutlineButton_2_clicked()
{
    for(int i = 0; i < ui->sectionListWidget->count(); i++){
        QString str = ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->commentTextEdit->document()->toPlainText();
        OutlineSection outline = underReviewOutlines.at(ui->outlines->currentRow())->getOutline().getOutlineSectionList().at(i);
        ui->sectionListWidget->setCurrentRow(i);
        outline.setComments(str);
    }
}

void ChairWindow::on_sumbitOutlineButton_2_clicked()
{
    underReviewOutlines.at(ui->outlines->currentRow())->getOutline().setStatus("approved");
}

void ChairWindow::on_cancelButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(CHAIR_INDEX);
}
void ChairWindow::on_ChairBackButton_3_clicked()
{
    this->close();
    MainLogin *w = new MainLogin(0, data);
    w->show();
}

